package com.suning.sawp.web.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.intf.logistics.LogisticsService;
import com.suning.sawp.vo.logistics.LogisticsRequest;
import com.suning.sawp.vo.logistics.LogisticsVo;
import com.suning.sawp.web.exception.MyGsonView;

@Controller
@RequestMapping("/logistics")
public class LogisticsController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogisticsController.class);

    @Autowired
    LogisticsService logisticsService;

    @RequestMapping(value = "/queryLogistics.do", method = RequestMethod.POST)
    public MyGsonView queryLogistics(String posOrderId, String storeCode) {
        LOGGER.debug("queryLogistics IN,posOrderId={},storeCode={}", posOrderId, storeCode);
        ReturnMsg<LogisticsVo> result = new ReturnMsg<LogisticsVo>();
        result.setRetFlag(ReturnMsg.FAIL);
        if (StringUtils.isNotBlank(posOrderId) || StringUtils.isNotBlank(storeCode)) {
            LogisticsVo logisticsVo = null;
            try {
                logisticsVo = logisticsService.queryLogisticsByPosOrderAndStoreCode(posOrderId, storeCode);
            } catch (Exception e) {
                LOGGER.error("query and tran logistics date is fail", e);
                result.setErrorMessage("物流数据查询失败！");
                return setGsonView(result);
            }
            result.setData(logisticsVo);
            result.setRetFlag(ReturnMsg.SUCCESS);
        } else {
            result.setErrorMessage("输入参数不能为空！");
        }

        LOGGER.debug("queryLogistics OUT");
        return setGsonView(result);
    }

    @RequestMapping(value = "/queryLogisticsNew.do", method = RequestMethod.POST)
    @ResponseBody
    public ReturnMsg<LogisticsVo> queryLogistics(@Valid LogisticsRequest logisticsRequest, BindingResult bindResult) {
        LOGGER.debug("queryLogisticsNew IN,logisticsRequest={}", logisticsRequest.toString());
        ReturnMsg<LogisticsVo> result = new ReturnMsg<LogisticsVo>();
        result.setRetFlag(ReturnMsg.FAIL);
        if (bindResult.hasErrors()) {
            result.setError(bindResult.getFieldError().getCode(), bindResult.getFieldError().getDefaultMessage());
            return result;
        }
        try {
            LogisticsVo logisticsVo = logisticsService.queryLogistics(logisticsRequest);
            result.setData(logisticsVo);
            result.setRetFlag(ReturnMsg.SUCCESS);
        } catch (Exception e) {
            LOGGER.error("新物流数据查询失败", e);
            result.setErrorMessage("物流数据查询失败！");
        }
        return result;

    }

}
